//: [Previous](@previous)

//import XCPlayground
import UIKit
import Foundation

//var str = "Hello, playground"

//XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
//let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
//view.backgroundColor = UIColor.whiteColor()
//XCPlaygroundPage.currentPage.liveView = view

//: 2.DateAndTimeHelper类
@objc class DateAndTimeHelper: NSObject {
    internal class func yearOfDate(date: NSDate, type: String...) -> String {
        let calendar = NSCalendar.currentCalendar()
        let component = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date)
        var result = ""
        type.forEach { (s: String) in
            if let r = component.valueForKey(s) {
                result = result + "-" + String(r)
            }
        }
        return result
    }
}
//let nowDate = NSDate.init()
//print(DateAndTimeHelper.yearOfDate(nowDate, type:"year", "day"))

//1.
@objc class playViewController: UIViewController {
    let datas = ["year", "month", "day", "hour", "minute", "second"]
    var titleLab = UILabel()
    var date = NSDate.init()
    var pickView = UIPickerView()
    let formmater = NSDateFormatter.init()
    
    override func viewDidLoad() {
        formmater.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let lab = UILabel(frame: CGRect(x: 50, y: 10, width: 300, height: 50))
        lab.text = formmater.stringFromDate(date)
        lab.textColor = UIColor.darkGrayColor()
        self.view.addSubview(lab)
        
        titleLab.frame = CGRect(x: 100, y: 70, width: 200, height: 30)
        titleLab.textColor = UIColor.redColor()
        self.titleLab.text = DateAndTimeHelper.yearOfDate(date, type: datas.first!)
        self.view.addSubview(titleLab)
        
        pickView.frame = CGRect(x: 200, y: 100, width: 100, height: 60)
        pickView.delegate = self
        pickView.dataSource = self
        self.view.addSubview(pickView)
    }
}

//let playVC = playViewController()
//view.addSubview(playVC.view)

extension playViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return datas.count
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return datas[row]
    }
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let str = datas[row]
        self.titleLab.text = DateAndTimeHelper.yearOfDate(date, type: str)
    }
}

//: [Next](@next)
